package com.ymatou.iapi.payment.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.iapi.payment.parameter.QueryPaymentStatusByTradingIdBean;
import com.ymatou.iapi.payment.service.CheckPaymentCall;
import com.ymatou.iapi.payment.service.QueryPaymentStatusByTradingIdCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 提交退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
//这个接口不会回写支付状态到数据库
public class Ts_QueryPaymentStatusByTradingId {
	private static QueryPaymentStatusByTradingIdBean QueryPaymentStatusByTradingIdBean;
	private static QueryPaymentStatusByTradingIdCall QueryPaymentStatusByTradingIdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询支付状态-H5实时");
	}

	@Before
	public void caseUp() {
		QueryPaymentStatusByTradingIdBean = new QueryPaymentStatusByTradingIdBean();
		QueryPaymentStatusByTradingIdCall = new QueryPaymentStatusByTradingIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(QueryPaymentStatusByTradingIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_001() {
		Logger.start(true, "查询交易成功-type4013");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4013");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_002() {
		Logger.start(true, "查询交易成功-type4016");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4016");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_003() {
		Logger.start(true, "查询交易成功-type4013,未支付且查询失败");
		try {
			//没有mock会失败
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4013");
			PayGateWayCallService.acquireOrder(aqb);
//			PayGateWayCallService.notifyCall(aqb);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(-1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_004() {
		Logger.start(true, "查询交易成功-type4013,db未支付checkpayment查询支付成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4013");
			PayGateWayCallService.acquireOrder(aqb);
//			PayGateWayCallService.notifyCall(aqb);
			
			Map alimock = getAlipayMockMap(aqb);
			addMock(QueryPaymentStatusByTradingIdCall, alimock);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_005() {
		Logger.start(true, "查询交易成功-type4013,db未支付checkpayment查询支付失败");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4013");
			PayGateWayCallService.acquireOrder(aqb);
//			PayGateWayCallService.notifyCall(aqb);
			
			Map alimock = getAlipayMockMap(aqb);
			alimock.put("MockResult-AliPay-is_success", "N");
			alimock.put("MockResult-AliPay-error", "TRADE_NOT_EXIST");
			alimock.put("MockResult-AliPay-trade_status", "TRADE_NOT_EXIST");
			addMock(QueryPaymentStatusByTradingIdCall, alimock);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(-1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_006() {
		Logger.start(true, "查询交易成功-type4016,db未支付checkpayment查询支付成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4016");
			PayGateWayCallService.acquireOrder(aqb);
//			PayGateWayCallService.notifyCall(aqb);
			
			Map alimock = getWeiXinMockMap(aqb);
			addMock(QueryPaymentStatusByTradingIdCall, alimock);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryPaymentStatusByTradingId_007() {
		Logger.start(true, "查询交易成功-type4016,db未支付checkpayment查询支付成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("4016");
			PayGateWayCallService.acquireOrder(aqb);
//			PayGateWayCallService.notifyCall(aqb);
			
			Map alimock = getWeiXinMockMap(aqb);
			alimock.put("MockResult-WeiXin-result_code", "FAIL");
			alimock.put("MockResult-WeiXin-trade_state", "NOTPAY");
			addMock(QueryPaymentStatusByTradingIdCall, alimock);
			QueryPaymentStatusByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryPaymentStatusByTradingIdCall.setData(QueryPaymentStatusByTradingIdBean);
			QueryPaymentStatusByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryPaymentStatusByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals(-1d, m.get("PayStatus"), "验证PayStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void addMock(QueryPaymentStatusByTradingIdCall cpcall, Map m) {
		Logger.comment("初始化MockHeader信息:" + m.toString());
		Set sl = m.entrySet();
		for (Object o : sl) {
			String[] ss = o.toString().split("=");
			String value = FormatUtil.urlEncode(ss[1]);
			cpcall.addHeader(ss[0], value);
		}
	}
	private Map getWeiXinMockMap(AcquireOrderBean aqb) throws Exception {
		Logger.comment("获取默认的WeiXinMock信息");
		Map m = new HashMap<String, String>();
		m.put("Mock", "1");
		String mockid = TestDataManager.getRandomUUID().toString();
		m.put("MockId", mockid);
		m.put("MockResult-WeiXin-return_code", "SUCCESS");
		m.put("MockResult-WeiXin-result_code", "SUCCESS");
		m.put("MockResult-WeiXin-trade_state", "SUCCESS");
		MPayment ppm = PayGateWayCallService.getPPayment(aqb);
		m.put("MockResult-WeiXin-total_fee", new BigDecimal(aqb.getPayPrice()).multiply(BigDecimal.valueOf(100)).setScale(0).toString());
		m.put("MockResult-WeiXin-refund_fee", new BigDecimal(aqb.getPayPrice()).multiply(BigDecimal.valueOf(100)).setScale(0).toString());
		m.put("MockResult-WeiXin-out_trade_no",ppm.getPaymentId());
		m.put("MockResult-WeiXin-settlement_total_fee", "-1");
		return m;
	}
	/**
	 * 获取默认的AlipayMock信息
	 * 
	 * @return
	 */
	private Map getAlipayMockMap(AcquireOrderBean aqb) {
		Logger.comment("获取默认的AlipayMock信息");
		Map m = new HashMap<String, String>();
		m.put("Mock", "1");
		String mockid = TestDataManager.getRandomUUID().toString();
		m.put("MockId", mockid);
		m.put("MockResult-AliPay-is_success", "T");
		m.put("MockResult-AliPay-error", "OK");
		m.put("MockResult-AliPay-trade_status", "TRADE_SUCCESS");
		m.put("MockResult-AliPay-total_fee",new BigDecimal(aqb.getPayPrice()).setScale(2).toString());
		return m;
	}
}
